import { SubscribeMessage, WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Socket, Server } from 'socket.io';

// 购物车同步的websocket通讯
@WebSocketGateway({ namespace: 'api/v1/cart' })
export class CartGateway {
  @WebSocketServer() wss: Server;

  @SubscribeMessage('addCart')
  handleMessage(_client: Socket, room: string): void {
    this.wss.emit('changeCard', room);
  }

  @SubscribeMessage('joinRoom')
  handleJoinRoom(client: Socket, room: string): void {
    client.join(room);
    console.log(room, '加入房间');
    client.emit('joinedRoom', room);
  }

  @SubscribeMessage('leaveRoom')
  handleRoomLeave(client: Socket, room: string): void {
    client.leave(room);
    client.emit('leftRoom', room);
  }
}
